package com.example.springboot.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.mapper.*;
import com.example.springboot.model.entity.CmsRole;
import com.example.springboot.model.form.Result;
import com.example.springboot.service.sys.PermissionListService;
import com.example.springboot.service.sys.RoleService;
import com.example.springboot.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private CmsUserRoleMapper cmsUserRoleMapper;

    @Resource
    private PermissionListService permissionListService;

    @Resource
    private CmsRolePermissionMapper cmsRolePermissionMapper;

    @Resource
    private CmsPermissionMapper cmsPermissionMapper;

    @Resource
    private CmsModelMapper cmsModelMapper;

    @Resource
    private CmsRoleMapper roleMapper;

    /**
     * @Description: 角色列表
     * @Param: [name]
     * @return: BaseResult
     * @Author: weiquan
     * @Date: 2019/7/1
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object getList(@RequestParam(value = "name", required = false) String name) {
        if (StringUtil.isBlank(name)) {
            name = "";
        }
        List<CmsRole> cmsRoleList = roleService.listByName(name);
        return Result.buildSuccess(cmsRoleList);
    }

    /**
     * @Description: 新增角色
     * @Param: cmsRole
     * @return: BaseResult
     * @Author: weiquan
     * @Date: 2019/7/1
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object saveRole(CmsRole cmsRole) {
        Integer integer = roleMapper.selectCount(new QueryWrapper<CmsRole>().lambda().eq(StringUtils.isNotBlank(cmsRole.getName()), CmsRole::getName, cmsRole.getName()));
        if (integer > 0) {
            return Result.buildFail("角色名已存在!");
        }
        boolean flag = roleService.save(cmsRole);
        if (flag) {
            return Result.buildSuccess();
        } else {
            return Result.buildFail();
        }

    }

    /**
     * @Description:修改角色
     * @Param: cmsRole
     * @return: BaseResult
     * @Author: weiquan
     * @Date: 2019/7/1
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateRole(CmsRole cmsRole) {
        boolean flag = roleService.updateById(cmsRole);
        if (flag) {
            return Result.buildSuccess();
        } else {
            return Result.buildFail();
        }
    }

    /**
     * @Description:删除角色
     * @Param: id
     * @return: BaseResult
     * @Author: weiquan
     * @Date: 2019/7/1
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result deleteRole(@RequestParam(value = "id", required = true) Long id) {
        log.info("删除的角色Id,{}", id);
        return roleService.deleteById(id);
    }

    /**
     * @Description:获取角色下拉框
     */
    @PostMapping("/getRoleBox")
    public Result getRoleBox() {
        return roleService.getRoleBox();
    }

    /**
     * 复制角色
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/copyRole", method = RequestMethod.POST)
    public Result copyRole(Long id) {
        return roleService.copyRole(id);
    }

}
